clear
clc
close all


filenames = getStaticTestFilenames('staticTestData','normal');

fuelmass = 1; %kg
[meanIsp,stdIsp,Isps,variousIsps] = calcIsps(filenames,fuelmass,0);

fprintf('Mean Isp = %f s, Standard Dev = %f\n',meanIsp,stdIsp);

figure
subplot(2,3,1)
hist(Isps)
title('I_{sp} Histogram with Final Force Zeroing')

subplot(2,3,2)
hist(variousIsps.init)
title('I_{sp} Histogram with Initial Force Zeroing')

subplot(2,3,3)
hist(variousIsps.lin)
title('I_{sp} Histogram with Linear Force Zeroing')

subplot(2,3,4)
hist(variousIsps.step)
title('I_{sp} Histogram with Linear-Step Force Zeroing')

subplot(2,3,5)
hist(variousIsps.base)
title('I_{sp} Histogram with No Zeroing')

xP = [min(variousIsps.base),max(variousIsps.base)];

psFin = polyfit(variousIsps.base,variousIsps.fin,1);
yFin = polyval(psFin,xP);

psInit = polyfit(variousIsps.base,variousIsps.init,1);
yInit = polyval(psInit,xP);

psLin = polyfit(variousIsps.base,variousIsps.lin,1);
yLin = polyval(psLin,xP);

psStep = polyfit(variousIsps.base,variousIsps.step,1);
yStep = polyval(psStep,xP);

figure
hold all
ph(1:2) = plot(variousIsps.base,variousIsps.fin,'r*',xP,yFin,'r-');

ph(3:4) = plot(variousIsps.base,variousIsps.init,'g*',xP,yInit,'g-');

ph(5:6) = plot(variousIsps.base,variousIsps.lin,'b*',xP,yLin,'b-');

ph(7:8) = plot(variousIsps.base,variousIsps.step,'m*',xP,yStep,'m-');

lvals = {'Removed Final Zero','Removed Initial Zero',...
    'Linear Decrease','Step Linear Decrease'};

xlabel('Base I_{sp}');
ylabel('Modified I_{sp}');
legend(ph([1,3,5,7]),lvals)
grid on

linStepDiff =  abs(variousIsps.lin - variousIsps.step);
meanLinStep = mean(linStepDiff);
devLinStep = std(linStepDiff);
approxLinStepDX = getDeltaXforDeltaIsp(mean(variousIsps.lin),meanLinStep);

fprintf('\nDifference between Linear and Linear-Step Zeroing;\n')
fprintf('Mean = %f s, Standard Deviation = %f s, \nApproximate change in flight distance = %f m\n',...
    meanLinStep,devLinStep,approxLinStepDX)

initFinDiff = abs(variousIsps.init - variousIsps.fin);
meanInitFin = mean(initFinDiff);
devInitFin = std(initFinDiff);
approxInitFinDX = getDeltaXforDeltaIsp(mean(variousIsps.init),meanInitFin);

fprintf('\nDifference between Initial Force and Final Force Zeroing;\n')
fprintf('Mean = %f s, Standard Deviation = %f s, \nApproximate change in flight distance = %f m\n',...
    meanInitFin,devInitFin,approxInitFinDX)

allDiffs = [(variousIsps.lin - variousIsps.base)',...
    (variousIsps.step - variousIsps.base)',...
    (variousIsps.init - variousIsps.base)',...
    (variousIsps.fin - variousIsps.base)'];

meanDiffs = mean(abs(allDiffs));
for i = 1:4
    meanDiffsDX(i) = getDeltaXforDeltaIsp(mean(variousIsps.base),meanDiffs(i));
end

maxDiffs = max(abs(allDiffs));
for i = 1:4
    maxDiffsDX(i) = getDeltaXforDeltaIsp(mean(variousIsps.base),maxDiffs(i));
end

fprintf('\nMean Differences from Base (no zeroing)\n')
fprintf('\t\t\tLinear\t\tLinear Step\tInitial Force\tFinal Force')
fprintf('\nImpulse (s)\t\t%f\t%f\t%f\t%f',meanDiffs(1),meanDiffs(2),meanDiffs(3),meanDiffs(4))
fprintf('\nApprox Distance (m)\t%f\t%f\t%f\t%f',meanDiffsDX(1),meanDiffsDX(2),meanDiffsDX(3),meanDiffsDX(4))

fprintf('\n\nMax Differences from Base (no zeroing)\n')
fprintf('\t\t\tLinear\t\tLinear Step\tInitial Force\tFinal Force')
fprintf('\nImpulse (s)\t\t%f\t%f\t%f\t%f',maxDiffs(1),maxDiffs(2),maxDiffs(3),maxDiffs(4))
fprintf('\nApprox Distance (m)\t%f\t%f\t%f\t%f\n',maxDiffsDX(1),maxDiffsDX(2),maxDiffsDX(3),maxDiffsDX(4))
